package net.sinmetal.twitcockpit.controller.cron.cockpit;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import net.sinmetal.twitcockpit.modelvar.AutoPostVar;
import net.sinmetal.twitcockpit.service.AutoPostService;
import net.sinmetal.twitcockpit.twitter.Twitter4tc;
import net.sinmetal.twitcockpit.twitter.Twitter4tcFactory;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.util.AppEngineUtil;
import org.slim3.util.TimeZoneLocator;

/**
 * CronSchedulePostController
 *
 * @author Sinmetal
 *
 */
public class SchedulePostController extends Controller {

    private AutoPostService autoPostService = new AutoPostService();

    /**
     * 定期PostをTweetする
     */
    @Override
    public Navigation run() throws Exception {
        TimeZone timeZone = TimeZoneLocator.get();
        Calendar cal = Calendar.getInstance(timeZone);

        List<AutoPostVar> autoTweets = autoPostService.getAutoTweets(cal);

        String twitterID = "test4sinmetal";
        if (AppEngineUtil.isProduction()) {
            twitterID = "chugokugtug";
        }
        Twitter4tc twitter4tc = new Twitter4tcFactory().getInstance(twitterID);
        for (AutoPostVar autoPostVar : autoTweets) {
            twitter4tc.updateStatus(autoPostVar.getTweet());
        }
        return null;
    }
}
